//
//  WBB_AlertView.m
//  QRCodeCar
//
//  Created by mac on 16/11/24.
//  Copyright © 2016年 desn. All rights reserved.
//

#import "WBB_AlertView.h"

@interface WBB_AlertView ()<UITextFieldDelegate>
@property (nonatomic, strong) UIViewController *viewController;
@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, strong) NSString *text;
@property (nonatomic, strong) UIAlertController
*alertController;
@end



@implementation WBB_AlertView

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];
}

+ (void)showAlertViewOnViewController:(UIViewController *)viewController title:(NSString *)title message:(NSString *)message  sureButtonClick:(void (^)())sureButtonClick canselButtonClick:(void(^)()) canselButtonCick{
    [self showAlertViewOnViewController:viewController title:title message:message sureButtonTitle:@"确定" canselButtonTitle:@"取消" sureButtonClick:sureButtonClick canselButtonClick:canselButtonCick];
}

/**
 alertView 自定义 确定和取消按钮文字
 
 @param viewController 在哪个控制器弹出
 @param title title
 @param message message
 @param sureTitle 确定titel
 @param canseltitle 取消title
 @param sureButtonClick 确定回调方法
 @param canselButtonCick 取消回调方法
 */
+ (void)showAlertViewOnViewController:(UIViewController *)viewController title:(NSString *)title message:(NSString *)message  sureButtonTitle:(NSString *)sureTitle canselButtonTitle:(NSString *)canseltitle sureButtonClick:(void (^)())sureButtonClick canselButtonClick:(void(^)()) canselButtonCick{
    
    UIAlertController *aletController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *canselAction = [UIAlertAction actionWithTitle:canseltitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        canselButtonCick();
    }];
    
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:sureTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        sureButtonClick();
    }];
    
    [aletController addAction:canselAction];
    [aletController addAction:sureAction];
    
    [viewController presentViewController:aletController animated:YES completion:nil];
}

//这个方法还没有完善，，，
+ (void)showAlertViewWithTextFieldOnViewController:(UIViewController *)viewController title:(NSString *)title message:(NSString *)message sureButtonClick:(void(^)())sureButtonClick canselButtonClick:(void(^)())canselButton{
    
    UIAlertController *aletController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    
    [aletController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        
    }];
    
    UIAlertAction *canselAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        canselButton();
    }];
    
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        sureButtonClick();
    }];
    
    [aletController addAction:canselAction];
    
    [aletController addAction:sureAction];
    
    [viewController presentViewController:aletController animated:YES completion:nil];
}

/**
 弹出视图 带有输入框
 
 使用这个方法,只能把该类作为 vc 的属性来调用 否则无效
 
 @param vc 控制器
 @param title 标题
 @param message 描述
 @param placeholder 提示输入
 @param sure 回调
 */
- (void)showTextAlertControllerOnViewController:(UIViewController *)vc title:(NSString *)title message:(NSString *)message placeHolder:(NSString *)placeholder sureBlock:(sureButtonClickBlock)sure{
    
    self.viewController = vc;
    
    
    self.alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UITextField *textField = self.alertController.textFields.firstObject;
        
        sure(textField.text);
        
    }];
    
    sureAction.enabled = NO;
    
    UIAlertAction *canselAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    __weak typeof(self) weakSelf = self;

    [self.alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        
        weakSelf.textField = textField;
        weakSelf.textField.keyboardType = UIKeyboardTypeNumberPad;
        weakSelf.textField.placeholder = placeholder;
        
        
        [[NSNotificationCenter defaultCenter] addObserver:weakSelf selector:@selector(textFeildValueChange:) name:UITextFieldTextDidChangeNotification object:weakSelf.textField];
      
        
    }];
    
    [self.alertController addAction:sureAction];
    [self.alertController addAction:canselAction];

    [vc presentViewController:self.alertController animated:YES completion:nil];
}



- (void)textFeildValueChange:(NSNotification *)notification{
    
    if (self.textField.text.length > 11) {
        self.textField.text = [self.textField.text substringToIndex:11];
    }
    
    UIAlertAction *sureAction = self.alertController.actions.firstObject;
    
    sureAction.enabled = (self.textField.text.length == 11);
    
}

+ (void)showWithTitle:(NSString *)title
              message:(NSString *)message
                style:(UIAlertControllerStyle)style
           showCancle:(BOOL)showCancle
         buttonTitles:(NSArray *)buttonTitles
        buttonActions:(NSArray *)buttonActions {
    NSAssert(buttonTitles.count == buttonActions.count, @"事件标题和内容数量不一致");
    UIAlertController *controller = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:style];
    for (int i=0; i<buttonTitles.count; i++) {
        [controller addAction:[UIAlertAction actionWithTitle:buttonTitles[i] style:UIAlertActionStyleDefault handler:buttonActions[i]]];
    }
    if (showCancle) {
        [controller addAction:[UIAlertAction actionWithTitle:@"取消"
                                                       style:UIAlertActionStyleCancel
                                                     handler:nil]];
    }
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:controller animated:YES completion:nil];
}


+ (void)showSheetAlertViewWithViewController:(UIViewController *)viewController Title:(NSString *)title message:(NSString *)message sheetTitleArr:(NSArray *)sheetArr complete:(void (^)(NSInteger index))complete{
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];
    
    NSMutableArray *actionArr = @[].mutableCopy;
    
    for (NSInteger i=0; i < sheetArr.count; i++) {
        UIAlertAction *action = [UIAlertAction actionWithTitle:sheetArr[i] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            complete(i);
        }];
        [actionArr addObject:action];
    }
    
    for (UIAlertAction *action in actionArr) {
        [alertController addAction:action];
    }
    
    [viewController presentViewController:alertController animated:YES completion:^{
        
    }];
}


@end
